#include "tileloader.h"
#include <QFile>
#include <QDir>

TileLoader::TileLoader(const QString &rootPath, QObject *parent)
    : QObject(parent), m_root(rootPath)
{
    m_diskCache.setCacheDirectory(QDir::tempPath() + "/qt_tiles");
    m_net.setCache(&m_diskCache);
}

QPixmap TileLoader::getTile(int z, int x, int y)
{
    quint64 key = (quint64(z) << 40) | (quint64(x) << 20) | y;
    if (QPixmap *cached = m_memCache.object(key))
        return *cached;

    QString path = QString("%1/%2/%3/%4.png")
                       .arg(m_root).arg(z).arg(x).arg(y);
    QPixmap pix(path);
    if (!pix.isNull())
        m_memCache.insert(key, new QPixmap(pix));
    return pix;
}
